package cn.mastor.basic.controller;

import cn.mastor.basic.config.BussinessException;
import cn.mastor.basic.domain.PhoneRegisterForm;
import cn.mastor.basic.util.MailUtil;
import cn.mastor.basic.util.StrUtils;
import cn.mastor.org.domain.Employee;
import cn.mastor.org.service.IVerifyCodeService;
import cn.mastor.basic.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IVerifyCodeService verifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String uuidKey){
//        System.out.println(uuidKey);
        String base64Code = verifyCodeService.getImageCode(uuidKey);
//        System.out.println(base64Code);
        return AjaxResult.me().success(base64Code);
    }
    @GetMapping("/image1/{key}")
    public AjaxResult getImageCode1(@PathVariable("key") String uuidKey){
//        System.out.println(uuidKey);
        String base64Code = verifyCodeService.getImageCode2(uuidKey);
//        System.out.println(base64Code);
        return AjaxResult.me().success(base64Code);
    }
    @PostMapping("/smsCode")
    public AjaxResult getMailCode(@RequestBody PhoneRegisterForm emp){
//        System.out.println(emp);
        if (StringUtils.isBlank(emp.getPhone())){
            throw new BussinessException("手机号不能为空");
        };
        if (emp.getPhone().length()!=11){
            throw new BussinessException("手机号不正确");
        }
        if(verifyCodeService.getMailCode(emp)==null){
            return AjaxResult.me().fail("验证码错误");
        }
        if (redisTemplate.getExpire(emp.getType()+emp.getPhone(), TimeUnit.MINUTES)>3){
            throw new BussinessException("请勿重复获取");
        }
        else {
            String mailCode = verifyCodeService.getMailCode(emp);
//            SendMsgUtil.sendSms(emp.getPhone(),mailCode);
            System.out.println("验证码："+mailCode);
            return AjaxResult.me().success("短信发送成功,四分钟内有效");
        }
    }
    @PostMapping("/smsCode2")
    public AjaxResult geteMailCode(@RequestBody PhoneRegisterForm emp){
        System.out.println(emp);
        if (StringUtils.isBlank(emp.getEmail())){
            throw new BussinessException("邮箱不能为空");
        }
        if(verifyCodeService.getEMailCode(emp)==null){
            return AjaxResult.me().fail("验证码错误");
        }
        if (redisTemplate.getExpire(emp.getType()+emp.getPhone(), TimeUnit.MINUTES)>3){
            throw new BussinessException("请勿重复获取");
        }
        else {
            String mailCode = verifyCodeService.getEMailCode(emp);
            MailUtil mailUtil = new MailUtil();
            mailUtil.sendMail("1303223697@qq.com","1303223697@qq.com","宠物之家短信验证码",mailCode,false);
            System.out.println("验证码："+mailCode);
            return AjaxResult.me().success("邮件发送成功,四分钟内有效");
        }
    }
    @GetMapping("/smsCode3/{key}")
    public AjaxResult geteMailCode2(@PathVariable("key") String phone){
        if (null==phone){
            throw new BussinessException("手机号不能为空");
        }
        String code = StrUtils.getComplexRandomString(4);
        System.out.println("短信验证码"+code);
        redisTemplate.opsForValue().set(phone,code);
        return AjaxResult.me().success("短信发送成功，四分钟内有效");
//        SendMsgUtil.sendSms(emp.getPhone(),mailCode);
    }
    private Employee verify(Employee emp) {
        if (StringUtils.isBlank(emp.getPhone())){
            throw new BussinessException("手机号不能为空");
        }
        return null;
    }
}
